package com.kuang.service;

import com.kuang.bean.User;
import com.kuang.bean.UserExample;
import com.kuang.dao.UserMapper;
import com.kuang.utils.GetTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public int addUser(User user) {
        return userMapper.insert(user);
    }

    public int deleteUserById(int id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    public int updateUser(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    public User queryUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public List<User> queryAllUser() {
        UserExample userExample = new UserExample();
        userExample.setOrderByClause("user_id asc");
        return userMapper.selectByExample(userExample);
    }

    public int queryCountById() {
        UserExample userExample = new UserExample();
        return (int)(userMapper.countByExample(userExample));
    }

    public List<User> queryByStatus() {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andStatusEqualTo("Y");
        return userMapper.selectByExample(userExample);
    }

    public List<User> queryByTime() {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        GetTime getTime = new GetTime();
        criteria.andCreateTimeBetween(getTime.getBefore(),getTime.getNow());
        return userMapper.selectByExample(userExample);
    }
}
